//
//  RCDPickerView.m
//  jsy
//
//  Created by  任辰东 on 2020/4/13.
//  Copyright © 2020 JSY. All rights reserved.
//

#import "RCDPickerView.h"
#import "NSString+Size.h"

@interface RCDPickerView ()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UIButton *cancelBtn;
@property (nonatomic, strong) UIButton *certainBtn;
@property (nonatomic, assign) BOOL isCompleteAnimation;
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, assign) NSInteger selectedIndex;
@property (nonatomic, copy) NSString *middleTitle;
@property (nonatomic, strong) UILabel *middleLabel;
@property (nonatomic, strong) UIView *middleLine;
@end

@implementation RCDPickerView

- (instancetype)initWithDataSourceArr:(NSArray *)dataSourceArr seletedRow:(NSInteger)index andtitle:( NSString * __nullable)title
{
    if (self = [super init]) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
        _dataSourceArr = dataSourceArr;
        _selectedIndex = index;
        _middleTitle = title;
        [self setupViews];
        [self setupLayout];
    }
    return self;
}

- (void)setupViews
{
    [self addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    
    self.containerView = [UIView new];
    self.containerView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.containerView];
    
    self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    //    self.cancelBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.cancelBtn setTitleColor:UIColorFromHex(0x999999) forState:UIControlStateNormal];
    self.cancelBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.cancelBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [self.containerView addSubview:self.cancelBtn];
    
    self.certainBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.certainBtn setTitle:@"确定" forState:UIControlStateNormal];
    //    self.certainBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [self.certainBtn setTitleColor:UIColorFromHex(0xff3300) forState:UIControlStateNormal];
    self.certainBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.certainBtn addTarget:self action:@selector(certainAction) forControlEvents:UIControlEventTouchUpInside];
    [self.containerView addSubview:self.certainBtn];
    
    if (!IsEmpty(self.middleTitle)) {
        self.middleLabel = [UILabel new];
        self.middleLabel.text = self.middleTitle;
        self.middleLabel.textAlignment = NSTextAlignmentCenter;
        self.middleLabel.textColor = UIColorFromHex(0x4D4D4D);
        self.middleLabel.font = [UIFont systemFontOfSize:18];
        [self.containerView addSubview:self.middleLabel];
    }
    
//    self.middleLine = [UIView new];
//    self.middleLine.backgroundColor = UIColorFromHex(kColorLine);
//    [self.containerView addSubview:self.middleLine];
    
    self.pickerView = [UIPickerView new];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    [self .containerView addSubview:self.pickerView];
    [self.pickerView selectRow:self.selectedIndex inComponent:0 animated:YES];
    
}

- (void)setupLayout
{
    WeakSelf
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        StrongSelf
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.width.mas_equalTo(kScreenWidth);
        make.top.equalTo(self.mas_bottom);
    }];
    
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        StrongSelf
        make.left.equalTo(self.containerView);
        make.width.mas_equalTo(60);
        make.top.equalTo(self.containerView).offset(10);
    }];
    
    [self.certainBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        StrongSelf
        make.right.equalTo(self.containerView);
        make.width.mas_equalTo(60);
        make.top.equalTo(self.containerView).offset(10);
    }];
    
    if (!IsEmpty(self.middleTitle)) {
        [self.middleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            StrongSelf
            make.centerX.equalTo(self.containerView.mas_centerX);
            make.centerY.equalTo(self.certainBtn.mas_centerY);
            
        }];
    }
    
//    [self.middleLine mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.cancelBtn.mas_bottom);
//        make.left.right.equalTo(self.containerView);
//        make.height.mas_equalTo(kOnePixel);
//    }];
    
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        StrongSelf
        make.top.equalTo(self.cancelBtn.mas_bottom);
        make.width.mas_equalTo(kScreenWidth);
        make.bottom.left.right.equalTo(self.containerView);
    }];
}

- (void)certainAction
{
    if (self.didSeclectedItem) {
        self.didSeclectedItem(self.selectedIndex, self.dataSourceArr[self.selectedIndex]);
    }
    [self dismiss];
}

-(void)setUnit:(NSString *)unit
{
    UILabel *label = [UILabel new];
    label.text = unit;
    label.font = UIFontSize(18);
    label.textColor = UIColorFromHex(0x000000);
    [self addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
       
        CGFloat width = [@"2021" widthWithFont:UIFontSize(18) constrainedToHeight:45];
        make.centerX.equalTo(self.pickerView).offset(width + 10);
        make.centerY.equalTo(self.pickerView);
        make.height.mas_equalTo(45);
        make.width.mas_equalTo(50);
    }];
}


- (void)show
{
    [kKeyWindow addSubview:self];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(kKeyWindow);
    }];
    
    // 提交固定layout
    [self layoutIfNeeded];
    
    WeakSelf
    [self.containerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        StrongSelf
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.width.mas_equalTo(kScreenWidth);
        make.bottom.equalTo(self);
    }];
    
    [UIView animateWithDuration:0.3 animations:^{
        StrongSelf
        self.isCompleteAnimation = NO;
        [self.containerView layoutIfNeeded];
    } completion:^(BOOL finished) {
        StrongSelf
        self.isCompleteAnimation = YES;
    }];
}

- (void)dismiss
{
    if (!self.isCompleteAnimation) {
        return;
    }
    
    [self removeFromSuperview];
}

#pragma mark - UIPickerViewDelegate

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 45;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.dataSourceArr.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return self.dataSourceArr[row];
}

//通过代理方法didSelectRows获取数据
 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
 {
     
     UILabel *pickerLabel =  (UILabel *)[pickerView viewForRow:row forComponent:component];
 
     pickerLabel.attributedText = [self pickerView:pickerView attributedTitleForRow:row forComponent:component];
     
     self.selectedIndex = row;
 }
 
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    [self changeSpearatorLineColor];
    
    /// 未选中颜色
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
//        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        pickerLabel.font = [UIFont systemFontOfSize:18];
        pickerLabel.textColor = [UIColor colorWithRed:12.f/255.f green:14.f/255.f blue:14.f/255.f alpha:1];
    }
    
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    
    return pickerLabel;
}
 
#pragma mark - 改变分割线的颜色
- (void)changeSpearatorLineColor {
    
    for(UIView *speartorView in _pickerView.subviews) {
 
        if (speartorView.frame.size.height < 80) {//找出当前的 View
            /// 背景
            // 添加分割线 (判断只添加一次  滑动不断刷新)
            if (speartorView.subviews.count ==0){
                UIView *line = [self lineView];
                line.frame = CGRectMake(0, 0, kScreenWidth, 0.5);
                [speartorView addSubview:line];
                
                UIView *line2 = [self lineView];
                line2.frame = CGRectMake(0, speartorView.mj_h-1, kScreenWidth, 0.5);
                [speartorView addSubview:line2];
            }
 
            speartorView.backgroundColor = [UIColor clearColor];
        }else{
            speartorView.backgroundColor = [UIColor clearColor];
        }
    }
}
/// 分割线
- (UIView *)lineView {
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)];
    line.backgroundColor = UIColorFromHex(kColorLine);
    return line;
}
 
 
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    NSString *titleString = [NSString stringWithFormat:@"%@",self.dataSourceArr[row]];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:titleString];
    NSRange range = [titleString rangeOfString:titleString];
    [attributedString addAttribute:NSForegroundColorAttributeName value:UIColorFromHex(0x000000) range:range];
    
    return attributedString;
}


@end
